<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddUser('add')">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增主页
        </el-button>
      </div>
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="desc" label="描述"></el-table-column>
        <el-table-column prop="cover" label="图片">
          <template #default="scope">
            <el-image :src="scope.row.cover" />
          </template>
        </el-table-column>
        <el-table-column prop="order" label="排序" width="50"></el-table-column>
        <el-table-column prop="createAt" label="创建时间" width="120"></el-table-column>
        <el-table-column prop="updateAt" label="更新时间" width="120"></el-table-column>
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <el-button size="small" type="primary" @click="onOpenEditUser('edit', scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes-options="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <HomeDialog ref="homeDialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import {deleHomeInfo, getedithome} from "./edithome";
import {delResource} from "/@/views/resource/resource";
// 引入组件
const HomeDialog = defineAsyncComponent(() => import("/@/views/edithome/dialog.vue"));

// 定义变量内容
const homeDialogRef = ref();
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10
    }
  }
});

// 初始化表格数据
const getTableData = () => {
  // state.tableData.loading = true;
  getedithome().then((res) => {
    state.tableData.data = res.baseInfo;
    state.tableData.total = res.total;
  });
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
  homeDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: any) => {
  homeDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: any) => {
deleHomeInfo(row.id).then((res) => {
  getTableData();
})
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  padding: 0px;

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
